package canhcuadaihoc.tracnghiemonline;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ContestActivity extends Activity{
	ViewSwitcher _ws;
	
	final static int SINGLE_CHOICE=0;
	final static int MULTI_CHOICE=1;
	
	Button bt_pre,bt_next,bt_pause,bt_play,bt_back,bt_finish;
	Chronometer _timer;
	
	AnswerKey_SingleChoice _ans_single;
	AnswerKey_MultiChoice _ans_multi;
	
	ViewGroup _container;

	CheckBox _chkA,_chkB,_chkC,_chkD,_chkE,_chkF,_chkG,_chkH;
	ArrayList<MultiChoice> array;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contest);
		
		//Bộ đếm giờ
		_timer=(Chronometer)findViewById(R.id.timer);
		setTimer();
		
		_ans_single=new AnswerKey_SingleChoice(this, null);
		_container=(ViewGroup)findViewById(R.id.ln_answerkey);
		_container.addView(_ans_single);
		
		bt_pre=(Button)findViewById(R.id.bt_pre);
		bt_next=(Button)findViewById(R.id.bt_next);
		bt_pause=(Button)findViewById(R.id.bt_pause);
		bt_play=(Button)findViewById(R.id.bt_play);
		bt_back=(Button)findViewById(R.id.bt_back_tab_TN);
		bt_finish=(Button)findViewById(R.id.bt_finish);
		
		
		bt_back.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		bt_pre.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Load_AnswerKey_Style(0);
			}
		});
		
		bt_next.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Load_AnswerKey_Style(1);
			}
		});

		bt_pause.setOnClickListener(StopCounter);
		
		bt_play.setOnClickListener(StartCounter);
		
		bt_finish.setOnClickListener(FinishTest);
		
	}
	
	public void setTimer(){
		long time=SystemClock.elapsedRealtime()-_timer.getBase();
		//Toast.makeText(getApplicationContext(), String.valueOf(time), Toast.LENGTH_SHORT).show();
		_timer.setBase(SystemClock.elapsedRealtime());
		_timer.start();
	}
	
	public void Pause_or_Start(int i){
		switch (i) {
		case 1:
			_timer.start();
			break;
		case 2:
			_timer.stop();
		default:
			break;
		}
	}
	
	public void Load_AnswerKey_Style(int i){
		
		switch (i) {
		case 0:
			_container.removeAllViews();
			_ans_single=new AnswerKey_SingleChoice(this, null);
			_container=(ViewGroup)findViewById(R.id.ln_answerkey);
			_container.addView(_ans_single);

			break;
		case 1:
			_container.removeAllViews();
			_ans_multi=new AnswerKey_MultiChoice(this, null);

			 _chkA=((AnswerKey_MultiChoice) _ans_multi).chk_A;
			 _chkB=((AnswerKey_MultiChoice) _ans_multi).chk_B;
			 _chkC=((AnswerKey_MultiChoice) _ans_multi).chk_C;
			 _chkD=((AnswerKey_MultiChoice) _ans_multi).chk_D;
			 _chkE=((AnswerKey_MultiChoice) _ans_multi).chk_E;
			 _chkF=((AnswerKey_MultiChoice) _ans_multi).chk_F;
			 _chkG=((AnswerKey_MultiChoice) _ans_multi).chk_G;
			 _chkH=((AnswerKey_MultiChoice) _ans_multi).chk_H;
			 
			 _chkE.setChecked(true);
			_container=(ViewGroup)findViewById(R.id.ln_answerkey);
			_container.addView(_ans_multi);
			
			break;

		default:
			break;
		}
		
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog=null;
		AlertDialog.Builder _alBuilder=new AlertDialog.Builder(ContestActivity.this);
		switch (id) {
		case 1:
			
			_alBuilder.setIcon(R.drawable.logo_small).setTitle("Thông Báo").setMessage("Bạn muốn tạm dừng tính giờ bài kiểm tra").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					ContestActivity.this.dismissDialog(1);
					Pause_or_Start(2);
				}
			});
			break;

		default:
			break;
		}
		dialog=_alBuilder.create();
		return dialog;
	};
	
	View.OnClickListener StartCounter=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Pause_or_Start(1);
		}
	};
	
	View.OnClickListener StopCounter=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			ContestActivity.this.showDialog(1);
		}
	};
	
	View.OnClickListener FinishTest=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent iDetailResults=new Intent(getApplicationContext(),DetailResultsAcitvity.class);
			startActivity(iDetailResults);
		}
	};
	/*
	 * 
	 * Hàm kiểm tra sự kiện Touch
	 * 
	 *
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Toast.makeText(getApplicationContext(), "DOWN", Toast.LENGTH_SHORT).show();
			break;
		case MotionEvent.ACTION_UP:
			Toast.makeText(getApplicationContext(), "UP", Toast.LENGTH_SHORT).show();
			break;
		case MotionEvent.ACTION_MOVE:
			Toast.makeText(getApplicationContext(), "MOVE", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);
	}
	
	
	 * 
	 * 
	 * 
	 * */
}
